/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/23
 * Time: 上午10:29
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Driver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Platform;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PageDriver {

    /*浏览器驱动*/
    private WebDriver driver = null;

    public WebDriver getDriver() {
        return driver;
    }

    /*各种构造方法*/
    public PageDriver() {
        driver = getLocalDriver(1);
    }

    public PageDriver(int browserType) {
        driver = getLocalDriver(browserType);
    }

    /*获取本地浏览器驱动*/
    private WebDriver getLocalDriver(int browserType) {
        switch (browserType) {
            case 1:
                return new FirefoxDriver();
            case 2:
                return new ChromeDriver();
            case 3:
                return new OperaDriver();
            case 4:
                return new SafariDriver();
            case 5:
                return new InternetExplorerDriver();
            case 6:
                return new EdgeDriver();
            default:
                return new FirefoxDriver();
        }
    }

    /*获取浏览器设定*/
    private DesiredCapabilities getCapability(int browserType, String browserVersion, int platformType) {
        DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
        Platform platform = Platform.WINDOWS;

        switch (browserType) {
            case 1:
                capability = DesiredCapabilities.firefox();
                capability.setBrowserName("firefox");
                break;
            case 2:
                capability = DesiredCapabilities.chrome();
                capability.setBrowserName("chrome");
                break;
            case 3:
                capability = DesiredCapabilities.operaBlink();
                capability.setBrowserName("opera");
                break;
            case 4:
                capability = DesiredCapabilities.safari();
                capability.setBrowserName("safari");
                break;
            case 5:
                capability = DesiredCapabilities.internetExplorer();
                capability.setBrowserName("internet explorer");
                capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                break;
            case 6:
                capability = DesiredCapabilities.edge();
                capability.setBrowserName("edge");
                break;
        }
        switch (platformType) {
            case 1:
                platform = Platform.MAC;
                break;
            case 2:
                platform = Platform.LINUX;
                break;
            case 3:
                platform = Platform.WINDOWS;
                break;
        }
        capability.setVersion(browserVersion);
        capability.setPlatform(platform);
        return capability;
    }
}
